/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.scripts.quests;

import l2s.commons.util.Rnd;
import l2s.gameserver.model.L2World;
import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.model.quest.QuestTimer;
import l2s.gameserver.scripting.ScriptFile;

/**
 * @author DreamBoss
 */
public class Q416_PathToOrcShaman extends Quest implements ScriptFile
{
	private static final String qn = "Q416_PathToOrcShaman";
	private static final int Hestui = 30585;
	private static final int HestuiTotemSpirit = 30592;
	private static final int SeerUmos = 30502;
	private static final int DudaMaraTotemSpirit = 30593;
	private static final int FireCharm = 1616;
	private static final int KashaBearPelt = 1617;
	private static final int KashaBladeSpiderHusk = 1618;
	private static final int FieryEgg1st = 1619;
	private static final int HestuiMask = 1620;
	private static final int FieryEgg2nd = 1621;
	private static final int TotemSpiritClaw = 1622;
	private static final int TatarusLetterOfRecommendation = 1623;
	private static final int FlameCharm = 1624;
	private static final int GrizzlyBlood = 1625;
	private static final int BloodCauldron = 1626;
	private static final int SpiritNet = 1627;
	private static final int BoundDurkaSpirit = 1628;
	private static final int DurkaParasite = 1629;
	private static final int TotemSpiritBlood = 1630;
	private static final int MaskOfMedium = 1631;
	private static final int KashaBear = 20479;
	private static final int KashaBladeSpider = 20478;
	private static final int ScarletSalamander = 20415;
	private static final int GrizzlyBear = 20335;
	private static final int VenomousSpider = 20038;
	private static final int ArachnidTracker = 20043;
	private static final int QuestMonsterDurkaSpirit = 27056;
	private static final int[][] DROPLIST_COND =
	{
		{
			1,
			0,
			KashaBear,
			FireCharm,
			KashaBearPelt,
			1,
			70,
			1
		},
		{
			1,
			0,
			KashaBladeSpider,
			FireCharm,
			KashaBladeSpiderHusk,
			1,
			70,
			1
		},
		{
			1,
			0,
			ScarletSalamander,
			FireCharm,
			FieryEgg1st,
			1,
			70,
			1
		},
		{
			6,
			7,
			GrizzlyBear,
			FlameCharm,
			GrizzlyBlood,
			3,
			70,
			1
		}
	};
	private static boolean QuestProf = true;
	
	public Q416_PathToOrcShaman(int questId, String name, String descr)
	{
		super(questId, name, descr);
		addStartNpc(Hestui);
		addTalkId(Hestui);
		addTalkId(HestuiTotemSpirit);
		addTalkId(SeerUmos);
		addTalkId(DudaMaraTotemSpirit);
		for (int[] element : DROPLIST_COND)
		{
			int questItems = 0;
			addKillId(element[2]);
			questItems = element[4];
			questItemIds = new int[]
			{
				questItems
			};
		}
		addKillId(VenomousSpider);
		addKillId(ArachnidTracker);
		addKillId(QuestMonsterDurkaSpirit);
		questItemIds = new int[]
		{
			FireCharm,
			HestuiMask,
			FieryEgg2nd,
			TotemSpiritClaw,
			TatarusLetterOfRecommendation,
			FlameCharm,
			BloodCauldron,
			SpiritNet,
			BoundDurkaSpirit,
			DurkaParasite,
			TotemSpiritBlood
		};
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (event.equalsIgnoreCase("30585-06.htm"))
		{
			st.giveItems(FireCharm, 1);
			st.set("cond", "1");
			st.setState(Quest.STATE_STARTED);
			st.playSound(QuestState.SOUND_ACCEPT);
		}
		else if (event.equalsIgnoreCase("30592-03.htm"))
		{
			st.takeItems(HestuiMask, -1);
			st.takeItems(FieryEgg2nd, -1);
			st.giveItems(TotemSpiritClaw, 1);
			st.set("cond", "4");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30585-11.htm"))
		{
			st.takeItems(TotemSpiritClaw, -1);
			st.giveItems(TatarusLetterOfRecommendation, 1);
			st.set("cond", "5");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30593-03.htm"))
		{
			st.takeItems(BloodCauldron, -1);
			st.giveItems(SpiritNet, 1);
			st.set("cond", "9");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30502-07.htm"))
		{
			st.takeItems(TotemSpiritBlood, -1);
			st.giveItems(MaskOfMedium, 1);
			st.playSound(QuestState.SOUND_FINISH);
			st.exitQuest(true);
		}
		if (event.equalsIgnoreCase("QuestMonsterDurkaSpirit_Fail"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(QuestMonsterDurkaSpirit);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
		}
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		int npcId = npc.getNpcId();
		String htmltext = "noquest";
		QuestState st = player.getQuestState(qn);
		int cond = st.getInt("cond");
		if (npcId == Hestui)
		{
			if (st.getQuestItemsCount(MaskOfMedium) != 0)
			{
				htmltext = "30502-04.htm";
				st.exitQuest(true);
			}
			else if (cond == 0)
			{
				if (st.getPlayer().getClassId().getId() != 0x31)
				{
					if (st.getPlayer().getClassId().getId() == 0x32)
					{
						htmltext = "30585-02a.htm";
					}
					else
					{
						htmltext = "30585-02.htm";
					}
					st.exitQuest(true);
				}
				else if (st.getPlayer().getLevel() < 19)
				{
					htmltext = "30585-03.htm";
					st.exitQuest(true);
				}
				else
				{
					htmltext = "30585-01.htm";
				}
			}
			else if (cond == 1)
			{
				htmltext = "30585-07.htm";
			}
			else if (cond == 2)
			{
				htmltext = "30585-08.htm";
				st.takeItems(KashaBearPelt, -1);
				st.takeItems(KashaBladeSpiderHusk, -1);
				st.takeItems(FieryEgg1st, -1);
				st.takeItems(FireCharm, -1);
				st.giveItems(HestuiMask, 1);
				st.giveItems(FieryEgg2nd, 1);
				st.set("cond", "3");
				st.setState(Quest.STATE_STARTED);
			}
			else if (cond == 3)
			{
				htmltext = "30585-09.htm";
			}
			else if (cond == 4)
			{
				htmltext = "30585-10.htm";
			}
			else if (cond == 5)
			{
				htmltext = "30585-12.htm";
			}
			else if (cond > 5)
			{
				htmltext = "30585-13.htm";
			}
		}
		else if (npcId == HestuiTotemSpirit)
		{
			if (cond == 3)
			{
				htmltext = "30592-01.htm";
			}
			else if (cond == 4)
			{
				htmltext = "30592-04.htm";
			}
		}
		else if ((npcId == HestuiTotemSpirit) && (st.getInt("cond") > 0) && ((st.getQuestItemsCount(GrizzlyBlood) > 0) || (st.getQuestItemsCount(FlameCharm) > 0) || (st.getQuestItemsCount(BloodCauldron) > 0) || (st.getQuestItemsCount(SpiritNet) > 0) || (st.getQuestItemsCount(BoundDurkaSpirit) > 0) || (st.getQuestItemsCount(TotemSpiritBlood) > 0) || (st.getQuestItemsCount(TatarusLetterOfRecommendation) > 0)))
		{
			htmltext = "30592-05.htm";
		}
		else if (npcId == SeerUmos)
		{
			if (cond == 5)
			{
				st.takeItems(TatarusLetterOfRecommendation, -1);
				st.giveItems(FlameCharm, 1);
				htmltext = "30502-01.htm";
				st.set("cond", "6");
				st.setState(Quest.STATE_STARTED);
			}
			else if (cond == 6)
			{
				htmltext = "30502-02.htm";
			}
			else if (cond == 7)
			{
				st.takeItems(GrizzlyBlood, -1);
				st.takeItems(FlameCharm, -1);
				st.giveItems(BloodCauldron, 1);
				htmltext = "30502-03.htm";
				st.set("cond", "8");
				st.setState(Quest.STATE_STARTED);
			}
			else if (cond == 8)
			{
				htmltext = "30502-04.htm";
			}
			else if ((cond == 9) || (cond == 10))
			{
				htmltext = "30502-05.htm";
			}
			else if (cond == 11)
			{
				htmltext = "30502-06.htm";
			}
		}
		else if (npcId == DudaMaraTotemSpirit)
		{
			if (cond == 8)
			{
				htmltext = "30593-01.htm";
			}
			else if (cond == 9)
			{
				htmltext = "30593-04.htm";
			}
			else if (cond == 10)
			{
				st.takeItems(BoundDurkaSpirit, -1);
				st.giveItems(TotemSpiritBlood, 1);
				htmltext = "30593-05.htm";
				st.set("cond", "11");
				st.setState(Quest.STATE_STARTED);
			}
			else if (cond == 11)
			{
				htmltext = "30593-06.htm";
			}
		}
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		int npcId = npc.getNpcId();
		QuestState st = checkPlayerState(player, npc, STATE_STARTED);
		if (st == null)
		{
			return null;
		}
		int cond = st.getInt("cond");
		for (int[] element : DROPLIST_COND)
		{
			if ((cond == element[0]) && (npcId == element[2]))
			{
				if ((element[3] == 0) || (st.getQuestItemsCount(element[3]) > 0))
				{
					if (element[5] == 0)
					{
						st.rollAndGive(element[4], element[7], element[6], QuestProf);
					}
					else if (st.rollAndGive(element[4], element[7], element[7], element[5], element[6], QuestProf))
					{
						if ((element[1] != cond) && (element[1] != 0))
						{
							st.set("cond", String.valueOf(element[1]));
							st.setState(Quest.STATE_STARTED);
						}
					}
				}
			}
		}
		if ((st.getQuestItemsCount(KashaBearPelt) != 0) && (st.getQuestItemsCount(KashaBladeSpiderHusk) != 0) && (st.getQuestItemsCount(FieryEgg1st) != 0))
		{
			st.set("cond", "2");
			st.setState(Quest.STATE_STARTED);
		}
		else if ((cond == 9) && ((npcId == VenomousSpider) || (npcId == ArachnidTracker)))
		{
			if (st.getQuestItemsCount(DurkaParasite) < 8)
			{
				st.giveItems(DurkaParasite, 1);
				st.playSound(QuestState.SOUND_ITEMGET);
			}
			if ((st.getQuestItemsCount(DurkaParasite) == 8) || ((st.getQuestItemsCount(DurkaParasite) >= 5) && Rnd.chance(st.getQuestItemsCount(DurkaParasite) * 10)))
			{
				L2Npc isQuest = L2World.getInstance().getNpcById(QuestMonsterDurkaSpirit);
				if (isQuest != null)
				{
					isQuest.deleteMe();
				}
				st.takeItems(DurkaParasite, -1);
				addSpawn(QuestMonsterDurkaSpirit, player.getX(), player.getY(), player.getZ(), player.getHeading(), false, 0, true);
				startQuestTimer("QuestMonsterDurkaSpirit_Fail", 300000, npc, player, false);
			}
		}
		else if (npcId == QuestMonsterDurkaSpirit)
		{
			QuestTimer timer = getQuestTimer("QuestMonsterDurkaSpirit_Fail", npc, player);
			if (timer != null)
			{
				timer.cancel();
			}
			L2Npc isQuest = L2World.getInstance().getNpcById(QuestMonsterDurkaSpirit);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			if (cond == 9)
			{
				st.takeItems(SpiritNet, -1);
				st.takeItems(DurkaParasite, -1);
				st.giveItems(BoundDurkaSpirit, 1);
				st.playSound(QuestState.SOUND_MIDDLE);
				st.set("cond", "10");
				st.setState(Quest.STATE_STARTED);
			}
		}
		return null;
	}
	
	public static void onLoad()
	{
		new Q416_PathToOrcShaman(416, qn, "Path To Orc Shaman");
	}
}
